define(function(require,exports,module){
	var aw = require("ajaxwrapper");
	var Handlebars = require("handlebars");
	var receiptPrintTpl = require("../assets/tpl/print.tpl");
	var productPageCtrl = require("../component/productPageCtrl");
	var paySvc = require('../store/paySvc');
	var Dialog = require("dialog");
	var store = require("store");
	var activeUser = store.get("user");
	var i18ns = require("i18n");
	require("printArea");
	
    module.exports = {
    	getMemberInfoByCard:function(widget,cardId){
    		//根据卡号获取会员信息，消费卡支付
    		paySvc.queryMemberByCard(cardId,function(data){
    			if(data[0] != null){
    				var status = data[0].accountStatus.key;
    			}
    			if(status != "Effective"){
    				Dialog.alert({
    					tip: "提示！",
    					content: "该卡为无效卡，无法完成付款！"
    				});
    				$("#cardNumber").val("");
    				$("#memberName").html("");
    				$("#balance").html("");
    				$("#pkAccount-form-group").html("");
    				$("#pkMember-form-group").html("");
    				return false;
    			}
               		$("#balance").html("");
                	$("#pkAccount-form-group").html("");
					$("#pkMember-form-group").html("");
		    		$("#memberName").html(data[0].member.personalInfo.name);
		    		$("#balance").html(data[0].balance);
		    		$("#pkAccount-form-group").html(data[0].pkAccount);
                	$("#memberPicture").prop("src","api/attachment/personalphoto/" + data[0].member.pkMember);
    		});
    	},
    	getMemberInfoByIdnumber:function(widget,idNumber){
    		//根据身份证号获取会员信息
    		paySvc.queryMemberById(idNumber,function(data){
                $("#pkAccount-form-group").html("");
                $("#pkMember-form-group").html("");
    			if(data && data[0]){
		    		$("#memberName").html(data[0].personalInfo.name);
		    		$("#pkMember-form-group").html(data[0].pkMember);
		    		$("#memberPicture").prop("src","api/attachment/personalphoto/" + data[0].pkMember);
				}
    		});
    	},
    	pay:function(widget){
    		var that = this;
    		//cartData存储所需要的数据
            var totalAmount = $("#totalAmount").val();
            //var cartData = localStorage.getItem("cartData");
   			//cartData = cartData?JSON.parse(cartData):{};
   			
   			var pkAccount = $("#pkAccount-form-group").html();
   			var pkMember = $("#pkMember-form-group").html();
   			if(!pkAccount && !pkMember){
   				Dialog.alert({
   				  title : "提示",
   				  content : "请先选择"+i18ns.get("member_name","顾客")+"再支付！",
   				});
   				return false;
   			}
			var balance = $("#balance").html();
   			if(isNaN(totalAmount) || totalAmount <= 0){
                Dialog.alert({
                    title : "提示",
                    content : "请输入正确的金额！",
                });
                return false;
			}
			//var totalAmount = $("#totalAmount").html();
			if($("#expenseCard").hasClass("active") 
					&& (parseFloat(balance) < parseFloat(totalAmount))){
				Dialog.alert({
					title : "提示",
					content : "账户余额不足请先充值！",
				});
				return false;
			}
            var saveData = {};
			if(pkAccount){
				saveData = {
						"account.pkAccount":pkAccount,
						money:totalAmount,
						description:"点餐刷卡！"
				};
			}else {
                saveData = {
                    "account.member.pkMember":pkMember,
                    money:totalAmount,
                    description:"点餐现金！"
                };
			}

			Dialog.alert({
				title : '提示',
				content : '支付中请稍候……',
				defaultButton : false
			});
			
			paySvc.createPreOrder(saveData,function(ret){
				Dialog.close();
                $("#cardNumber").val("");
                $("#memberName").html("");
                $("#balance").html("");
                $("#totalAmount").val("");
                $("#idNumber").val("");
                $("#pkAccount-form-group").html("");
                $("#pkMember-form-group").html("");
                $("#memberPicture").prop("src","assets/eling/elcms/pos/productitem/src/assets/img/header.png");
                Dialog.alert({
                    title : "提示",
                    content : "付款成功",
                });

			},function(){
				Dialog.close();
                $("#cardNumber").val("");
                $("#memberName").html("");
                $("#balance").html("");
                $("#totalAmount").val("");
                $("#idNumber").val("");
                $("#pkAccount-form-group").html("");
                $("#pkMember-form-group").html("");
				Dialog.alert({
					title : "提示",
					content : "付款失败",
				});
			});
    	},
    	print : function(cartData,orderData){
    		 var orderContentsList = [];
    		 
    		 var mealRecordList = cartData.mealRecordList;
			 for(var i = 0; i<mealRecordList.length; i++){
        		 orderContentsList.push({
        			 name: mealRecordList[i].name,
        			 count: mealRecordList[i].count,
        			 price: mealRecordList[i].price.toFixed(2),
        		 });
        	 }
			 
        	 var sendUrlArr = {
        			 organization : store.get("user").organization.name,
        			 servicePoint : store.get("user").servicePoint.name,
        			 payTime : moment().format("YYYY-MM-DD HH:mm:ss"),
        			 operator : store.get("user").name,
        			 member : $("#memberName").html(),
        			 code : moment().format("x"),
        			 paymentMethod : $(".pay-type-button.active").html(),
        			 price : parseFloat(cartData.totalAmount).toFixed(2),
        			 orderContents : orderContentsList
        	 };
        	 
        	 //刷消费卡 小票显示账户余额
    		 sendUrlArr["balance"] = cartData.balance?parseFloat(cartData.balance).toFixed(2):"0.00"
        	 
        	 //receiptPrintTpl = receiptPrintTpl.replace(/会员/g,i18ns.get("member_name","会员"));//替换Tpl内的字符串
 			 $(".hidden-print").html(Handlebars.compile(receiptPrintTpl)(sendUrlArr)); 
 			 $(".receipt-print").removeClass("hidden");
 			 var options = {mode: "popup", popClose: true};
 			 $(".receipt-print").printArea(options);
 			 $(".receipt-print").addClass("hidden");
    	},
    	userMe : function(){
    		paySvc.userMe();
    	}
    };
});
